package com.example.myapplication.MainFragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.myapplication.KeChengFragment.KeChengJingPinBanJiFragment;
import com.example.myapplication.KeChengFragment.MingShiKeChengFragment;
import com.example.myapplication.R;

public class FragmentKeCheng extends Fragment {
    RadioGroup KeCheng_rg;
    RadioButton KeCheng_rb1,KeCheng_rb2;
    View KeCheng_view1,KeCheng_view2;
    //fragment事务类
    FragmentManager KeCheng_fm;
    //fragment管理类
    FragmentTransaction KeCheng_ft;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_kecheng,container,false);
        //初始化事务需要获取getActivity
        KeCheng_fm=getActivity().getSupportFragmentManager();
        //开启事务
        KeCheng_ft=KeCheng_fm.beginTransaction();
        KeCheng_ft.replace(R.id.KeCheng_fl,new KeChengJingPinBanJiFragment());
        //提交
        KeCheng_ft.commit();
        initView(view);
        KeCheng_rb1.setTextSize(20);
        KeCheng_view1.setSelected(true);
        return view;
    }

    private void initView(View view) {
        //绑定控件
        KeCheng_rg=view.findViewById(R.id.KeCheng_rg);
        KeCheng_rb1=view.findViewById(R.id.KeCheng_rb1);
        KeCheng_rb2=view.findViewById(R.id.KeCheng_rb2);
        KeCheng_view1=view.findViewById(R.id.KeCheng_View1);
        KeCheng_view2=view.findViewById(R.id.KeCheng_View2);
        KeCheng_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                //再此开启事务
                KeCheng_ft=KeCheng_fm.beginTransaction();
                NoSelect();
                switch (i){
                    case R.id.KeCheng_rb1:
                        KeCheng_ft.replace(R.id.KeCheng_fl,new KeChengJingPinBanJiFragment());
                        KeCheng_rb1.setTextSize(20);
                        KeCheng_view1.setSelected(true);
                        break;
                    case R.id.KeCheng_rb2:
                        KeCheng_ft.replace(R.id.KeCheng_fl,new MingShiKeChengFragment());
                        KeCheng_rb2.setTextSize(20);
                        KeCheng_view2.setSelected(true);
                        break;

                }
                //提交
                KeCheng_ft.commit();
            }
        });
    }

    private void NoSelect() {
        KeCheng_rb1.setTextSize(15);
        KeCheng_view1.setSelected(false);
        KeCheng_rb2.setTextSize(15);
        KeCheng_view2.setSelected(false);
    }
}
